/*
 * Copyright 2011 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package com.yangfei.SportAssistant.util;

import com.yangfei.SportAssistant.services.ITrackRecordingService;
import com.yangfei.SportAssistant.services.TrackRecordingService;
import com.yangfei.SportAssistant.services.TrackRecordingServiceConnection;

import android.app.ActivityManager;
import android.app.ActivityManager.RunningServiceInfo;
import android.content.ComponentName;
import android.content.Context;
import android.os.RemoteException;
import android.util.Log;

import java.util.List;

public class TrackRecordingServiceConnectionUtils
{

	private static final String TAG = TrackRecordingServiceConnectionUtils.class.getSimpleName();

	private TrackRecordingServiceConnectionUtils()
	{}

	/**
	 * Returns true if the recording service is running.
	 * 
	 * @param context
	 *          the current context
	 */
	public static boolean isRecordingServiceRunning(Context context)
	{
		ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
		List<RunningServiceInfo> services = activityManager.getRunningServices(Integer.MAX_VALUE);

		for (RunningServiceInfo serviceInfo : services)
		{
			ComponentName componentName = serviceInfo.service;
			String serviceName = componentName.getClassName();
			if (TrackRecordingService.class.getName().equals(serviceName))
			{
				return true;
			}
		}
		return false;
	}

	/**
	 * Returns true if recording. Checks with the track recording service if
	 * available. If not, checks with the shared preferences.
	 * 
	 * @param context
	 *          the current context
	 * @param trackRecordingServiceConnection
	 *          the track recording service connection
	 */
	public static boolean isRecording(Context context, TrackRecordingServiceConnection trackRecordingServiceConnection)
	{
		ITrackRecordingService trackRecordingService = trackRecordingServiceConnection.getServiceIfBound();
		if (trackRecordingService != null)
		{
			try
			{
				return trackRecordingService.isRecording();
			}
			catch (RemoteException e)
			{
				Log.e(TAG, "Failed to check if service is recording", e);
			}
			catch (IllegalStateException e)
			{
				Log.e(TAG, "Failed to check if service is recording", e);
			}
		}
		return PreferencesUtils.getRecordingTrackId(context) != -1L;
	}

	/**
	 * Stops the track recording service connection.
	 * 
	 * @param context
	 *          the context
	 * @param trackRecordingServiceConnection
	 *          the track recording service connection
	 */
	public static void stop(Context context, TrackRecordingServiceConnection trackRecordingServiceConnection)
	{
		ITrackRecordingService trackRecordingService = trackRecordingServiceConnection.getServiceIfBound();
		long recordingTrackId = -1;
		if (trackRecordingService != null)
		{
			try
			{
				/*
				 * Need to remember the recordingTrackId before calling endCurrentTrack.
				 * endCurrentTrack sets the value to -1L.
				 */
				recordingTrackId = PreferencesUtils.getRecordingTrackId(context);
				trackRecordingService.endCurrentTrack();			
			}
			catch (Exception e)
			{
				Log.e(TAG, "Unable to stop recording.", e);
			}
		}
		else
		{
			PreferencesUtils.setRecordingTrackId(context, -1L);
		}
		trackRecordingServiceConnection.stop();
	}

	/**
	 * Resumes the track recording service connection.
	 * 
	 * @param context
	 *          the context
	 * @param trackRecordingServiceConnection
	 *          the track recording service connection
	 */
	public static void resume(Context context, TrackRecordingServiceConnection trackRecordingServiceConnection)
	{
		trackRecordingServiceConnection.bindIfRunning();
		if (!isRecordingServiceRunning(context))
		{
			PreferencesUtils.setRecordingTrackId(context, -1L);
		}
	}
}
